<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：IIdentity.class.php
 * @类功能: 解析身份证信息
 * @开发者: 陈旭林
 * @开发时间： 14-10-28
 * @版本：version 1.0
 */
namespace app\common\ext;

use app\common\enum\Sex;

class IIdentity {
    /**
     * @功能 取得身份证含有信息
     * @param int $identity 身份证号码
     * @return null
     */
    public static function getInfo($identity){
        //判断身份证是否正确
        if(strlen($identity) != 18){
            return false;
        }

        //取得出生年份
        $year = intval(substr($identity,6,4));
        $month = intval(substr($identity,10,2));
        $day = intval(substr($identity,12,2));
        $age = intval(intval(date('Y', time())) - $year);
        if(($year < 1900 || $year > 2200) || ($month < 1 || $month > 12) || ($day < 1 || $day > 31) || ($age < 1 || $age > 150)){
            return false;
        }

        //取得性别
        $sex_name = array(Sex::woman=>"女",Sex::man=>"男");
        $sex = intval(intval(substr($identity,16,1)) % 2);
        $sex = intval(($sex == 0) ? Sex::woman : Sex::man);

        //取得出生年月日
        $date = mktime(0,0,0,$month,$day,$year);
        $date = date('Y-m-d',$date);

        //整理数据
        $data['age'] = $age;
        $data['sex'] = $sex;
        $data['sex_name'] = $sex_name[$sex];
        $data['date'] = $date;

        //返回值
        return $data;
    }
}